import axios from '~/plugins/axios';
const TestIp = 'http://192.168.61.39:8080'
/**
 * 用户列表接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const fetchUserDatas = function({commit, state}, preload) {
    axios.get(process.env.baseUrl + "/member/list", {
        params: preload || {}
    }).then(function(response) {
        if (!response) {
            return
        }
        if (response.data.code == 0 && response.data.data) {
            commit('dealTotal', {
                total: response.data.data.total
            })
            commit('dealUserData', {
                data: response.data.data.list
            })
        }else{
            commit('dealTotal', {
                total: 0
            })
            commit('dealUserData', {
                data: []
            })
        }
    })
}

/**
 * 冻结用户接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const freezeStatus = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/frozenUser", {
        id: preload.id
    })
}
/**
 * 停用用户接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const disableStatus = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/stopUser", {
        id: preload.id
    })
}
/**
 * 启用用户接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const regainStatus = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/regainUser", {
        id: preload.id
    })
}

/**
 * 删除用户数据接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const deleteUser = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/deleteUser", {
        id: preload.id
    })
}
/**
 * 重置密码接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const resetPasswd = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/resetPasswd", preload)
}

/**
 * 修改密码接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const changePasswd = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/updatePasswd", preload)
}

/**
 * 修改用户角色接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const updateRole = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/updateJob", preload)
}
/**
 * 添加用户接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const addNewUserControl = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/insert", preload)
}
/**
 * 编辑用户接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const editUserData = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/update", preload)
}
/**
 * 添加用户时验证必选字段数据是否有效
 *
 * @param commit
 * @param state
 * @param preload
 */
export const validNewUser = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/member/haveMember", preload)
}
/**
 * 上传用户图片
 *
 * @param commit
 * @param state
 * @param preload
 */
export const userPhotoUpload = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + "/admin/shop/information/photoUpload", preload)
}

export const getJobList = function({commit, state}, preload) {
    return axios.get(process.env.baseUrl + "/job/typeList", {
        params: preload || {}
    }).then(response => {

        if (!response) {
            return
        }
        let len = response.data.data.length;

        let arr = [];
        for (let i = 0; i < len; i++) {
            let job = {
                value: response.data.data[i],
                label: response.data.data[i],
                level: 1,
                children: []
            };
            arr.push(job)
        }
        commit('dealJobList', {
            data: arr
        })
    })
}